import BaseMapHandler from './BaseMapHandler';

export default class MapTypeSwitchHandler extends BaseMapHandler {
  name = '地图类型切换'
  type = 'map-type-switch'
  description = '切换地图显示类型'
  active = false
  private mapType: number = 1 // 1:普通地图, 2:卫星地图, 3:混合地图
  private mapTypes: Array<{
    id: number
    name: string
    type: any
  }> = []

  constructor() {
    super()
    // 初始化地图类型数组
    const qq = (window as any).qq
    if (qq && qq.maps) {
      this.mapTypes = [
        {
          id: 1,
          name: '普通地图',
          type: qq.maps.MapTypeId.ROADMAP
        },
        {
          id: 2,
          name: '卫星地图',
          type: qq.maps.MapTypeId.SATELLITE
        },
        {
          id: 3,
          name: '混合地图',
          type: qq.maps.MapTypeId.HYBRID
        }
      ]
    }
  }

  activate(): void {
    this.active = true
  }

  deactivate(): void {
    this.active = false
  }

  handle(): void {
    // 切换到下一种地图类型
    this.switchMapType()
  }

  private switchMapType(): void {
    const map = (window as any).mapController.getMap()
    if (!map) {
      console.error('地图实例不存在')
      return
    }

    // 更新地图类型
    this.mapType = this.mapType % 3 + 1
    const selectedMapType = this.mapTypes.find(type => type.id === this.mapType)
    
    if (selectedMapType) {
      map.setMapTypeId(selectedMapType.type)
      console.log(`已切换到${selectedMapType.name}`)
    }
  }

  getCurrentMapType(): { id: number, name: string } {
    const selectedMapType = this.mapTypes.find(type => type.id === this.mapType)
    return selectedMapType || {
      id: 1,
      name: '普通地图'
    }
  }
}